<template>
	<div class="archives_patrolinfo_info">
		<div>
			<Table
				:tableData="tableData"
				:tableHead="tableHead"
				:isUseSlot="false"
				:page="listQuery.page"
				:limit="listQuery.limit"
				v-loading="listLoading"
			>
				<template slot="tool" slot-scope="{ data }">
					<el-button
						style="padding:3px;"
						icon="el-icon-document"
						@click="handleEdit(data.row)"
						type="text"
						>查看</el-button
					>
				</template>
			</Table>
			<Pagination
				v-show="total > 0"
				:total="total"
				:page.sync="listQuery.page"
				:limit.sync="listQuery.limit"
				@pagination="getList"
			></Pagination>
		</div>
		<form-tem
			:info="{ title: '用源设备信息', width: '70%', top: '5%' }"
			:formList="formList"
			@submit="submit"
			ref="formDelg"
			@onClose="closeDialog"
			@change="changeSource"
			:dialogFormVisible.sync="dialogFormVisible"
			:isDisabled="true"
		>
		</form-tem>
	</div>
</template>

<script>
import Pagination from "@/components/Pagination"; // 分页
import formTem from "@/template/form.temp";
import {
	getUseSourceDeviceList,
	insertUseSourceDevice,
	getUseSourceDeviceInfo,
	updateUseSourceDevice
} from "@/api/RCIM/RCIM-archives";
import Table from "@/components/Table";
import control from "../mixins/control";
import {
	getRadUsed,
	getDeviceManufacturer,
	getUsedStatus,
	getDataGatheringDeviceInfo,
	getRayDeviceType,
	getRayDeviceUse,
} from "@/api/RCIM/RCIM-select";
import { cloneDeep } from "lodash";
import { deleteUseSourceDevice } from "@/api/RCIM/RCIM-delet";
import { validateFormNumber } from "@/utils/validateForm";
export default {
	components: {
		Table,
		Pagination,
		formTem
	},
	mixins: [control],
	name: "archives_usesource_info",
	data() {
		return {
			tableData: [],
			tableHead: [
				{
					prop: "instrument",
					label: "数采仪",
					align: "left"
				},
				{
					prop: "radUsedName",
					label: "设备类型",
					align: "left"
				},
				{
					prop: "installPlace",
					label: "安装位置",
					align: "left"
				},
				{
					prop: "brand",
					label: "品牌",
					align: "left"
				},
				{
					prop: "operate",
					label: "操作",
					align: "center"
				}
			],
			total: 1,
			listQuery: {
				page: 1,
				limit: 10
			},
			listLoading: false,
			dialogFormVisible: false,
			formList: [
				{
					list: [
						{
							type: "select",
							label: "数采仪",
							width: "33%",
							labelWidth: "140px",
							placeholder: "请选择",
							moduleKey: "instrument",
							children: "option",
							options: [],
							query: {
								enterpriseId: this.entData.id
							},
							fetch: getDataGatheringDeviceInfo,
							// validate: function() {
							// 	return [
							// 		{
							// 			required: false,
							// 			message: "请选择数采仪",
							// 			trigger: "change"
							// 		}
							// 	];
							// }
						},
						{
							type: "input",
							label: "作业巡检间隔（小时）",
							width: "33%",
							labelWidth: "180px",
							placeholder: "",
							moduleKey: "jobInterval",
							validate: function() {
								return [
									{ validator: validateFormNumber, trigger: "blur" }
								]
							}
						},
						{ width: "33%" },
						{
							type: "select",
							label: "用源设备",
							width: "33%",
							labelWidth: "140px",
							placeholder: "",
							moduleKey: "useSourceDevice",
							children: "option",
							options: [
								{
									label: "源库",
									id: "1"
								},
								{
									label: "工作场所",
									id: "2"
								},
								{
									label: "车辆",
									id: "3"
								}
							],
							fetch: getRadUsed,
							validate: function() {
								return [
									{
										required: false,
										message: "请选择用源设备",
										trigger: "change"
									}
								];
							}
						},
						{
							type: "input",
							label: "出厂编号",
							width: "33%",
							labelWidth: "140px",
							placeholder: "",
							maxlength: "20",
							showWordLimit: true,
							moduleKey: "factoryNo",
							// validate: function() {
							// 	return [
							// 		{
							// 			required: false,
							// 			message: "请输入出厂编号",
							// 			trigger: "blur"
							// 		}
							// 	];
							// }
						},
						{
							type: "select",
							label: "品牌",
							labelWidth: "140px",
							width: "34%",
							labelWidth: "140px",
							placeholder: "",
							moduleKey: "brand",
							children: "option",
							options: [
								{
									label: "源库",
									id: "1"
								},
								{
									label: "工作场所",
									id: "2"
								},
								{
									label: "车辆",
									id: "3"
								}
							],
							fetch: getDeviceManufacturer,
							// validate: function() {
							// 	return [
							// 		{
							// 			required: false,
							// 			message: "请选择品牌",
							// 			trigger: "change"
							// 		}
							// 	];
							// }
						},
						{
							type: "input",
							label: "唯一身份码",
							labelWidth: "140px",
							width: "33%",
							placeholder: "",
							moduleKey: "RFID",
							maxlength: "20",
							showWordLimit: true,
							validate: function() {
								return [
									{
										required: false,
										message: "请输入唯一身份码",
										trigger: "blur"
									}
								];
							}
						},
						{
							type: "input",
							label: "安装位置",
							width: "33%",
							labelWidth: "140px",
							placeholder: "",
							maxlength: "20",
							showWordLimit: true,
							moduleKey: "installPlace",
							validate: function() {
								return [
									{
										required: false,
										message: "请输入安装位置",
										trigger: "blur"
									}
								];
							}
						},

						{ width: "34%" },

						{
							type: "radio-group",
							label: "在用状态",
							width: "100%",
							placeholder: "",
							labelWidth: "140px",
							moduleKey: "status",
							children: "radio",
							// width: '100%',
							defaultValue: [],
							options: [],
							fetch: getUsedStatus,
							validate: function() {
								return [
									{
										required: false,
										message: "请选择在用状态",
										trigger: "change"
									}
								];
							}
						},
						{
							type: "select",
							label: "类型",
							width: "100%",
							width: "33%",
							placeholder: "",
							labelWidth: "140px",
							moduleKey: "IsMove",
							children: "option",
							options: [
								{
									label: "固定源",
									value: '0'
								},
								{
									label: "移动源",
									value: '1'
								},
								{
									label: "射线装置",
									value: '2'
								},
								{
									label: "环境剂量仪",
									value: '3'
								}
							],
							defaultValue: '0',
							validate: function() {
								return [
									{
										required: false,
										message: "请选择类型",
										trigger: "change"
									}
								];
							}
						}
						// {
						// 	type: "switch",
						// 	label: "是否移动源",
						// 	width: "100%",
						// 	placeholder: "",
						// 	labelWidth: "140px",
						// 	moduleKey: "IsMove",
						// 	defaultValue: 0,
						// 	activeValue: 1,
						// 	inactiveValue: 0
						// }
					]
				}
			],
			formListOld: [
				{
					list: [
						{
							type: "select",
							label: "数采仪",
							width: "33%",
							labelWidth: "140px",
							placeholder: "请选择",
							moduleKey: "instrument",
							children: "option",
							options: [],
							query: {
								enterpriseId: this.entData.id
							},
							fetch: getDataGatheringDeviceInfo,
							// validate: function() {
							// 	return [
							// 		{
							// 			required: false,
							// 			message: "请选择数采仪",
							// 			trigger: "change"
							// 		}
							// 	];
							// }
						},
						{
							type: "input",
							label: "作业巡检间隔（小时）",
							width: "33%",
							labelWidth: "180px",
							placeholder: "",
							moduleKey: "jobInterval",
							validate: function() {
								return [
									{ validator: validateFormNumber, trigger: "blur" }
								]
							}
						},
						{ width: "33%" },
						{
							type: "select",
							label: "用源设备",
							width: "33%",
							labelWidth: "140px",
							placeholder: "",
							moduleKey: "useSourceDevice",
							children: "option",
							options: [
								{
									label: "源库",
									id: "1"
								},
								{
									label: "工作场所",
									id: "2"
								},
								{
									label: "车辆",
									id: "3"
								}
							],
							fetch: getRadUsed,
							validate: function() {
								return [
									{
										required: false,
										message: "请选择用源设备",
										trigger: "change"
									}
								];
							}
						},
						{
							type: "input",
							label: "出厂编号",
							width: "33%",
							labelWidth: "140px",
							placeholder: "",
							maxlength: "20",
							showWordLimit: true,
							moduleKey: "factoryNo",
							// validate: function() {
							// 	return [
							// 		{
							// 			required: false,
							// 			message: "请输入出厂编号",
							// 			trigger: "blur"
							// 		}
							// 	];
							// }
						},
						{
							type: "select",
							label: "品牌",
							labelWidth: "140px",
							width: "34%",
							labelWidth: "140px",
							placeholder: "",
							moduleKey: "brand",
							children: "option",
							options: [
								{
									label: "源库",
									id: "1"
								},
								{
									label: "工作场所",
									id: "2"
								},
								{
									label: "车辆",
									id: "3"
								}
							],
							fetch: getDeviceManufacturer,
							// validate: function() {
							// 	return [
							// 		{
							// 			required: false,
							// 			message: "请选择品牌",
							// 			trigger: "change"
							// 		}
							// 	];
							// }
						},
						{
							type: "input",
							label: "唯一身份码",
							labelWidth: "140px",
							width: "33%",
							placeholder: "",
							moduleKey: "RFID",
							maxlength: "20",
							showWordLimit: true,
							validate: function() {
								return [
									{
										required: false,
										message: "请输入唯一身份码",
										trigger: "blur"
									}
								];
							}
						},
						{
							type: "input",
							label: "安装位置",
							width: "33%",
							labelWidth: "140px",
							placeholder: "",
							maxlength: "20",
							showWordLimit: true,
							moduleKey: "installPlace",
							validate: function() {
								return [
									{
										required: false,
										message: "请输入安装位置",
										trigger: "blur"
									}
								];
							}
						},

						{ width: "34%" },

						{
							type: "radio-group",
							label: "在用状态",
							width: "100%",
							placeholder: "",
							labelWidth: "140px",
							moduleKey: "status",
							children: "radio",
							// width: '100%',
							defaultValue: [],
							options: [],
							fetch: getUsedStatus,
							validate: function() {
								return [
									{
										required: false,
										message: "请选择在用状态",
										trigger: "change"
									}
								];
							}
						},
						{
							type: "select",
							label: "类型",
							width: "100%",
							width: "33%",
							placeholder: "",
							labelWidth: "140px",
							moduleKey: "IsMove",
							children: "option",
							options: [
								{
									label: "固定源",
									value: '0'
								},
								{
									label: "移动源",
									value: '1'
								},
								{
									label: "射线装置",
									value: '2'
								},
								{
									label: "环境剂量仪",
									value: '3'
								}
							],
							defaultValue: '0',
							validate: function() {
								return [
									{
										required: false,
										message: "请选择类型",
										trigger: "change"
									}
								];
							}
						}
						// {
						// 	type: "switch",
						// 	label: "是否移动源",
						// 	width: "100%",
						// 	placeholder: "",
						// 	labelWidth: "140px",
						// 	moduleKey: "IsMove",
						// 	defaultValue: 0,
						// 	activeValue: 1,
						// 	inactiveValue: 0
						// }
					]
				}
			],
			formListDevice: [
				{
					list: [
						{
							type: "select",
							label: "数采仪",
							width: "33%",
							labelWidth: "140px",
							placeholder: "请选择",
							moduleKey: "instrument",
							children: "option",
							options: [],
							query: {
								enterpriseId: this.entData.id
							},
							fetch: getDataGatheringDeviceInfo,
							// validate: function() {
							// 	return [
							// 		{
							// 			required: false,
							// 			message: "请选择数采仪",
							// 			trigger: "change"
							// 		}
							// 	];
							// }
						},
						{
							type: "input",
							label: "作业巡检间隔（小时）",
							width: "33%",
							labelWidth: "180px",
							placeholder: "",
							moduleKey: "jobInterval",
							validate: function() {
								return [
									{ validator: validateFormNumber, trigger: "blur" }
								]
							}
						},
						{ width: "33%" },
						{
							type: "select",
							label: "用源设备",
							width: "33%",
							labelWidth: "140px",
							placeholder: "",
							moduleKey: "useSourceDevice",
							children: "option",
							options: [],
							fetch: getRadUsed,
							validate: function() {
								return [
									{
										required: false,
										message: "请选择用源设备",
										trigger: "change"
									}
								];
							}
						},
						{
							type: "input",
							label: "出厂编号",
							width: "33%",
							labelWidth: "140px",
							placeholder: "",
							maxlength: "20",
							showWordLimit: true,
							moduleKey: "factoryNo",
							// validate: function() {
							// 	return [
							// 		{
							// 			required: false,
							// 			message: "请输入出厂编号",
							// 			trigger: "blur"
							// 		}
							// 	];
							// }
						},
						{
							type: "select",
							label: "品牌",
							labelWidth: "140px",
							width: "34%",
							labelWidth: "140px",
							placeholder: "",
							moduleKey: "brand",
							children: "option",
							options: [
								{
									label: "源库",
									id: "1"
								},
								{
									label: "工作场所",
									id: "2"
								},
								{
									label: "车辆",
									id: "3"
								}
							],
							fetch: getDeviceManufacturer,
							// validate: function() {
							// 	return [
							// 		{
							// 			required: false,
							// 			message: "请选择品牌",
							// 			trigger: "change"
							// 		}
							// 	];
							// }
						},
						{
							type: "input",
							label: "唯一身份码",
							labelWidth: "140px",
							width: "33%",
							placeholder: "",
							moduleKey: "RFID",
							maxlength: "20",
							showWordLimit: true,
							validate: function() {
								return [
									{
										required: false,
										message: "请输入唯一身份码",
										trigger: "blur"
									}
								];
							}
						},
						{
							type: "input",
							label: "安装位置",
							width: "33%",
							labelWidth: "140px",
							placeholder: "",
							maxlength: "20",
							showWordLimit: true,
							moduleKey: "installPlace",
							validate: function() {
								return [
									{
										required: false,
										message: "请输入安装位置",
										trigger: "blur"
									}
								];
							}
						},

						{ width: "34%" },

						{
							type: "input",
							label: "射线装置型号",
							width: "33%",
							labelWidth: "140px",
							placeholder: "请输入射线装置型号",
							maxlength: "20",
							showWordLimit: true,
							moduleKey: "rayDeviceModel",
							validate: function() {
								return [
									{
										required: false,
										message: "请输入射线装置型号",
										trigger: "blur"
									}
								];
							}
						},
						{
							type: "select",
							label: "射线装置类别",
							width: "33%",
							placeholder: "",
							fetchTimes: Infinity,
							labelWidth: "140px",
							moduleKey: "rayDeviceTypeId",
							children: "option",
							fetch: getRayDeviceType,
							options: [],
							validate: function() {
								return [
									{
										required: false,
										message: "请选择射线装置类别",
										trigger: "change"
									}
								];
							}
						},
						{
							type: "select",
							label: "射线装置用途",
							width: "33%",
							placeholder: "",
							labelWidth: "140px",
							moduleKey: "rayDeviceUseId",
							fetch: getRayDeviceUse,
							fetchTimes: Infinity,
							children: "option",
							options: [],
							validate: function() {
								return [
									{
										required: false,
										message: "请选择射线装置用途",
										trigger: "change"
									}
								];
							}
						},

						{
							type: "radio-group",
							label: "在用状态",
							width: "100%",
							placeholder: "",
							labelWidth: "140px",
							moduleKey: "status",
							children: "radio",
							// width: '100%',
							defaultValue: [],
							options: [],
							fetch: getUsedStatus,
							validate: function() {
								return [
									{
										required: false,
										message: "请选择在用状态",
										trigger: "change"
									}
								];
							}
						},
						{
							type: "select",
							label: "类型",
							width: "33%",
							placeholder: "",
							labelWidth: "140px",
							moduleKey: "IsMove",
							children: "option",
							options: [
								{
									label: "固定源",
									value: '0'
								},
								{
									label: "移动源",
									value: '1'
								},
								{
									label: "射线装置",
									value: '2'
								},
								{
									label: "环境剂量仪",
									value: '3'
								}
							],
							defaultValue: '0',
							validate: function() {
								return [
									{
										required: false,
										message: "请选择类型",
										trigger: "change"
									}
								];
							}
						},
						// {
						// 	type: "switch",
						// 	label: "是否移动源",
						// 	width: "100%",
						// 	placeholder: "",
						// 	labelWidth: "140px",
						// 	moduleKey: "IsMove",
						// 	defaultValue: 0,
						// 	activeValue: 1,
						// 	inactiveValue: 0
						// }
					]
				}
			],
			search: "",
			editInfo: {}
		};
	},
	mounted() {
		this.listQuery.id = this.entData.id;
		this.getList();
	},
	methods: {
		/**
		 * @desc 删除数据
		 * @param {Object} row 表格行数据
		 */
		delet(row) {
			this.$confirm("确认要删除吗？")
				.then(_ => {
					deleteUseSourceDevice(row.id).then(res => {
						if (res.code === 200) {
							this.getList();
							return this.$message({
								type: "success",
								message: "删除成功"
							});
						}
						return this.$message({
							type: "error",
							message: "删除失败"
						});
					});
				})
				.catch(_ => {});
		},
		/**
		 * @desc 表单联动函数
		 * @param {String} keys key
		 * @param {String} value 
		 */
		changeSource(keys, value) {
			if (keys === "IsMove" && value === '1') {
				const newForm = cloneDeep(this.formListOld);
				newForm[0].list[newForm[0].list.length - 3] = {
					type: "input",
					label: "MN号",
					width: "34%",
					labelWidth: "140px",
					maxlength: "20",
					showWordLimit: true,
					placeholder: "",
					moduleKey: "MN",
					// validate: function() {
					// 	return [
					// 		{
					// 			required: false,
					// 			message: "请输入MN号",
					// 			trigger: "blur"
					// 		}
					// 	];
					// }
				};
				// newForm[0].list[0] = {
				// 	type: "select",
				// 	label: "数采仪",
				// 	width: "33%",
				// 	labelWidth: "140px",
				// 	placeholder: "请选择",
				// 	moduleKey: "instrument",
				// 	children: "option",
				// 	options: [],
				// 	query: {
				// 		enterpriseId: this.entData.id
				// 	},
				// 	fetch: getDataGatheringDeviceInfo,
				// };
				this.formList = newForm;

				this.$nextTick(() => {
					this.$refs.formDelg.getFormEle().forEach(item => {
						const value = item[0].form;
						item[0].deleteKeys(["rayDeviceTypeId", "rayDeviceUseId"])
					});
				})
			} else if(keys === "IsMove" && (value === '0' || value === '3') ) {
				const newForm = cloneDeep(this.formListOld);
				newForm[0].list[newForm[0].list.length - 3] = {width: '34%'}
				// newForm[0].list[0] = {
				// 	type: "select",
				// 	label: "数采仪",
				// 	width: "33%",
				// 	labelWidth: "140px",
				// 	placeholder: "请选择",
				// 	moduleKey: "instrument",
				// 	children: "option",
				// 	options: [],
				// 	query: {
				// 		enterpriseId: this.entData.id
				// 	},
				// 	fetch: getDataGatheringDeviceInfo,
				// 	validate: function() {
				// 		return [
				// 			{
				// 				required: false,
				// 				message: "请选择数采仪",
				// 				trigger: "change"
				// 			}
				// 		];
				// 	}
				// }
				this.formList = newForm;
				this.$nextTick(() => {
					this.$refs.formDelg.getFormEle().forEach(item => {
						const value = item[0].form;
						item[0].deleteKeys(["rayDeviceTypeId", "rayDeviceUseId"])
					});
				})
			} else if(keys === "IsMove" && value === '2') {
				const newForm = cloneDeep(this.formListDevice);
				this.formList = newForm;
			}
		},
		/**
		 * @desc 获取数据
		 */
		async getList() {
			this.listLoading = true;
			const { data } = await getUseSourceDeviceList(this.listQuery);
			this.total = data.total;
			this.tableData = data.useSourceDeviceList;
			this.listLoading = false;
		},
		/**
		 * @desc 编辑查看表格数据
		 * @param {Object} row 表格行数据
		 */
		async handleEdit(row) {
			this.$refs.formDelg.openDialog();
			const { data } = await getUseSourceDeviceInfo({ id: row.id });
			this.editInfo = data.data;
			const parent = this.$refs.formDelg.getFormEle().forEach(item => {
				data.data.IsMove += '';
				item[0].setDefaultValue(data.data);
				this.changeSource('IsMove', data.data.IsMove += '')
			});
		},
		/**
		 * @desc 关闭弹窗回调
		 */
		closeDialog() {
			// debugger
			this.editInfo = {}
			this.formList = cloneDeep(this.formListOld)
			// this.$emit("closeDetail", false)
			// this.dialogFormVisible = false;
		},
		/**
		 * @desc 新增、编辑数据
		 * @param {Object} formData 表单数据
		 */
		submit(params) {
			const formData = cloneDeep(params)
			if( formData.IsMove === '0' || formData.IsMove === '2' ) formData.MN = '';
			formData.IsMove = formData.IsMove - 0;
			formData.entId = this.entData.id;
			if (this.editInfo.id) {
				this.update(formData);
			} else {
				insertUseSourceDevice(formData).then(res => {
					this.fresh();
					if (res.code === 200) {
						this.dialogFormVisible = false;
						return this.$message({
							type: "success",
							message: "添加成功"
						});
					}
					return this.$message({
						type: "success",
						message: "添加失败"
					});
				});
			}
		},
		/**
		 * @desc 编辑数据
		 * @param {Object} params 表单数据
		 */
		update(params) {
			params.id = this.editInfo.id;
			updateUseSourceDevice(params).then(res => {
				this.fresh();
				if (res.code === 200) {
					this.dialogFormVisible = false;
					return this.$message({
						type: "success",
						message: "修改成功"
					});
				}
				return this.$message({
					type: "success",
					message: "修改失败"
				});
			});
		},
		/**
		 * @desc 初始化表单数据
		 * @param {Object} val 表单数据
		 */
		initShowDetail(val) {
			if (val) {
				let data = {
					IsMove: "0",
					radio: "",
					installPlace: "",
					instrument: "",
					useSourceDevice: "",
					factoryNo: "",
					brand: "",
					RFID: ""
				};
				// this.$nextTick(() => {
				const parent = this.$refs.formDelg
					.getFormEle()
					.forEach(item => {
						item[0].setDefaultValue(data);
					});
				// })
				this.changeSource('IsMove', "0");
			}
		}
	}
};
</script>

<style lang="scss" scoped>
.archives_patrolinfo_info {
}
</style>
